<template>
    <popup
        ref="popupRef"
        title="司机证件"
        :async="true"
        width="550px"
        @confirm="handleConfirm"
        @close="popupClose"
    >
        <el-form ref="formRef" :model="formData" label-width="120px" :rules="formRules">
            <el-form-item label="身份证号" prop="id_card">
                <el-input v-model="formData.id_card" clearable placeholder="请输入身份证号码" />
            </el-form-item>
            <el-form-item label="驾驶证照片" prop="driver_license_pic">
                <material-picker v-model="formData.driver_license_pic" />
            </el-form-item>
            <el-form-item label="驾驶证有效期" prop="driver_license_valid_date">
                <el-date-picker
                    class="flex-1 !flex"
                    v-model="formData.driver_license_valid_date"
                    clearable
                    type="date"
                    :disabled-date="disabledDate"
                    value-format="YYYY-MM-DD"
                    placeholder="选择驾驶证有效期"
                >
                </el-date-picker>
            </el-form-item>
            <el-form-item label="行驶证照片" prop="vehicle_license_pic">
                <material-picker v-model="formData.vehicle_license_pic" />
            </el-form-item>
            <el-form-item label="行驶证有效期" prop="vehicle_license_valid_date">
                <el-date-picker
                    class="flex-1 !flex"
                    v-model="formData.vehicle_license_valid_date"
                    clearable
                    type="date"
                    :disabled-date="disabledDate"
                    value-format="YYYY-MM-DD"
                    placeholder="选择行驶证有效期"
                >
                </el-date-picker>
            </el-form-item>
            <el-form-item label="车牌号" prop="car_num">
                <el-input v-model="formData.car_num" clearable placeholder="请输入车牌号" />
            </el-form-item>
            <el-form-item label="网约驾驶证" prop="network_license_pic">
                <material-picker v-model="formData.network_license_pic" />
            </el-form-item>
            <el-form-item label="网约车驾驶证" prop="network_licensee_pic">
                <material-picker v-model="formData.network_licensee_pic" />
            </el-form-item>
            <el-form-item label="车辆照片" prop="car_pic">
                <material-picker :limit="3" v-model="formData.car_pic" />
            </el-form-item>
            <el-form-item label="车辆类型" class="w-[260px]" prop="car_type_id">
                <el-select v-model="formData.car_type_id" placeholder="请选择车辆类型">
                    <el-option
                        v-for="(item, index) in optionsData.car_type"
                        :key="index"
                        :label="item.name"
                        :value="item.id"
                    >
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="状态" class="w-[260px]" prop="status">
                <el-select v-model="formData.status" placeholder="请选择状态">
                    <el-option
                        v-for="(item, index) in dictData.approval_status"
                        :key="index"
                        :label="item.name"
                        :value="parseInt(item.value)"
                    >
                    </el-option>
                </el-select>
            </el-form-item>
        </el-form>
    </popup>
</template>

<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { useDictData, useDictOptions } from '@/hooks/useDictOptions'
import { carTypeAll } from '@/api/car_type.ts'
import { apiDriverCredentialDetail } from '@/api/user'

const formRef = shallowRef<FormInstance>()
const props = defineProps({
    show: {
        type: Boolean,
        required: true
    },
    value: {
        type: [Number, String],
        required: true
    }
})

// 获取字典数据
const { dictData } = useDictData('approval_status')

const emit = defineEmits<{
    (event: 'update:show', value: boolean): void
    (event: 'confirm', value: any): void
}>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()

// 表单数据
const formData = reactive({
    id: '',
    id_card: '',
    driver_license_pic: '',
    driver_license_valid_date: '',
    vehicle_license_pic: '',
    vehicle_license_valid_date: '',
    driver_license_image: '',
    network_license_pic: '',
    network_licensee_pic: '',
    car_pic: '',
    car_num: '',
    car_type_id: '',
    status: ''
})

// 表单验证
const formRules = reactive<any>({
    id_card: [
        {
            required: true,
            message: '请输入身份证号码',
            trigger: ['blur']
        }
    ],
    driver_license_pic: [
        {
            required: true,
            message: '请选择驾驶证照片',
            trigger: ['blur']
        }
    ],
    driver_license_valid_date: [
        {
            required: true,
            message: '请选择驾驶证有效期',
            trigger: ['blur']
        }
    ],
    vehicle_license_pic: [
        {
            required: true,
            message: '请选择行驶证照片',
            trigger: ['blur']
        }
    ],
    vehicle_license_valid_date: [
        {
            required: true,
            message: '请选择行驶证有效期',
            trigger: ['blur']
        }
    ],
    network_license_pic: [
        {
            required: true,
            message: '请选择网约驾驶证',
            trigger: ['blur']
        }
    ],
    network_licensee_pic: [
        {
            required: true,
            message: '请选择网约车行驶证',
            trigger: ['blur']
        }
    ],
    car_num: [
        {
            required: true,
            message: '请输入车牌号',
            trigger: ['blur']
        }
    ],
    car_type_id: [
        {
            required: true,
            message: '请选择车辆类型',
            trigger: ['blur']
        }
    ]
})

// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await apiDriverCredentialDetail({
        id: row
    })
    setFormData(data)
}

// 提交按钮
const handleConfirm = async () => {
    try {
        await formRef.value?.validate()
        emit('confirm', formData)
    } catch (error) {}
}

const { optionsData } = useDictOptions<{
    car_type: any[]
}>({
    car_type: {
        api: carTypeAll
    }
})
const halfAYearLater = new Date()
halfAYearLater.setMonth(halfAYearLater.getMonth() + 6)
const disabledDate = (time: Date) => {
    return time.getTime() <= halfAYearLater.getTime()
}

const popupClose = () => {
    emit('update:show', false)
    formRef.value?.resetFields()
}

watch(
    () => props.show,
    (val) => {
        if (val) {
            getDetail(props.value)
            popupRef.value?.open()
        } else {
            popupRef.value?.close()
        }
    }
)
defineExpose({
    setFormData,
    getDetail
})
</script>
